package org.wdcode.web.socket.server;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;

import org.wdcode.common.exception.CustomRuntimeException;
import org.wdcode.web.socket.client.SocketClient;

/**
 * Socket服务器实现
 * @author WD
 * @since JDK6
 * @version 1.0 2010-01-28
 */
final class SocketServerImpl implements SocketServer {
	// JDK ServerSocket
	private ServerSocket	server;

	/**
	 * 构造参数
	 */
	public SocketServerImpl() {
		try {
			server = new ServerSocket();
		} catch (IOException e) {
			throw new CustomRuntimeException(e);
		}
	}

	/**
	 * 构造参数
	 * @param port 端口
	 */
	public SocketServerImpl(int port) {
		try {
			server = new ServerSocket(port);
		} catch (IOException e) {
			throw new CustomRuntimeException(e);
		}
	}

	/**
	 * 侦听并接受到此Socket的连接
	 * @return SocketClient
	 */
	public SocketClient accept() {
		return null;
	}

	/**
	 * 绑定到一台服务器上
	 * @param host 服务器地址
	 * @param port 服务器端口
	 */
	public void bind(int port) {
		try {
			server.bind(new InetSocketAddress(port));
		} catch (IOException e) {
			throw new CustomRuntimeException(e);
		}
	}

	/**
	 * 关闭资源
	 */
	public void close() {
		try {
			server.close();
		} catch (IOException e) {
			throw new CustomRuntimeException(e);
		}
	}
}
